<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class uplog extends \plugin{
	public function addgenjin_log($postdata,$storeid=0){
		if(!$postdata['remark']){
			exi('请填写跟进内容！','error');
		}
		$postdata['memberid']=$postdata['memberid']?:intval($postdata['id']);
		$data=array(
			'memberid'=>$postdata['memberid'],
			'remark'=>$postdata['remark'],
			'status'=>$postdata['status'],
			'type'=>$postdata['type'],
		);
		if($postdata['huifang_time']){
			$data['huifang_time']=strtotime($postdata['huifang_time']);
		}
		$id=intval($postdata['id']);
		if($id && !$postdata['memberid']){
			$data['up_time']=time();
			pdo_update('jy_yxw_genjin_log',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['workerid']=$postdata['workerid']?:$_SESSION['workerid'];
			$data['storeid']=$storeid?:$_SESSION['storeid'];
			$data['createtime']=time();
			pdo_insert('jy_yxw_genjin_log',$data);
			$id=pdo_insertid();
		}
		return $id;
	}
	public function upclasskaoqing_log($classid=0,$day=''){
		$day=$day?:date('Y-m-d',time());
		$class_kc=pdo_get('jy_yxw_class_kc',array('day'=>$day,'classid'=>$classid,'uniacid'=>$_SESSION['uniacid']));
		if(!$class_kc['id']){
			return false;
		}
		$params=array('status'=>12,'classid'=>$classid,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$list=pdo_getall('jy_yxw_yonghu',$params,array('id'));
		foreach($list as $item){
			$isadd=pdo_count('jy_yxw_kaoqing_log',array('day'=>$day,'yonghuid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isadd){
				$this->addkaoqing_log($item['id'],0,$day,'',$class_kc['fuwuid'],$class_kc['kejianid']);
			}
		}
		return true;
	}
	public function addkaoqing_log($yonghuid,$type=0,$day='',$remark='',$fuwuid=0,$kejianid=0){
		if($day){
			$isadd=pdo_count('jy_yxw_kaoqing_log',array('day'=>$day,'yonghuid'=>$yonghuid,'uniacid'=>$_SESSION['uniacid']),'id');
		}
		if($isadd){
			exi('当天已考勤过了','error');
		}
		$data=array(
			'type'=>$type,
			'remark'=>$remark,
		);
		if(!$day){
			$day=date('Y-m-d',time());
			pdo_update('jy_yxw_yonghu',array('kaoqing_day'=>$day),array('id'=>$yonghuid,'uniacid'=>$_SESSION['uniacid']));
		}
		$isadd=pdo_count('jy_yxw_kaoqing_log',array('day'=>$day,'yonghuid'=>$yonghuid,'uniacid'=>$_SESSION['uniacid']),'id');
		if($isadd){
			$data['up_time']=time();
			pdo_update('jy_yxw_kaoqing_log',$data,array('day'=>$day,'yonghuid'=>$yonghuid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['yonghuid']=$yonghuid;
			$data['storeid']=$storeid?:$_SESSION['storeid'];
			$data['day']=$day;
			$data['classid']=$this->session('classid');
			$data['fuwuid']=$fuwuid;
			$data['kejianid']=$kejianid;
			$data['createtime']=time();
			pdo_insert('jy_yxw_kaoqing_log',$data);
			$id=pdo_insertid();
		}
		return $id;
	}
	public function upclassdabian_log($listid){
		$item=pdo_get('jy_yxw_dabian_list',array('id'=>$listid,'uniacid'=>$_SESSION['uniacid']));
		$yonghus=pdo_getall('jy_yxw_yonghu',array('classid'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($yonghus as $v){
			$this->updabian_log($v,$item);
		}
		return true;
	}
	public function updabian_log($yonghu,$item){
		$data=array();
		$isadd=pdo_count('jy_yxw_dabian_log',array('listid'=>$item['id'],'yonghuid'=>$yonghu['id'],'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$isadd){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['yonghuid']=$yonghu['id'];
			$data['storeid']=$item['storeid'];
			$data['listid']=$item['id'];
			$data['classid']=$item['classid']?:$this->session('classid');
			$data['createtime']=time();
			pdo_insert('jy_yxw_dabian_log',$data);
		}
		return true;
	}
	public function upkaoqing_log($post){
		$data=array(
			'type'=>$post['type'],
			'remark'=>$post['remark'],
		);
		if($post['id']){
			$data['up_time']=time();
			pdo_update('jy_yxw_kaoqing_log',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['yonghuid']=$post['yonghuid'];
			$data['storeid']=$post['storeid'];
			$data['classid']=$post['classid']?:$this->session('classid');
			$data['createtime']=time();
			pdo_insert('jy_yxw_weiji_log',$data);
		}
		return true;
	}
	public function upfenfee_log($infee_log=array()){
		if(!$infee_log['pay_type']){
			return false;
		}
		$isadd=pdo_get('jy_yxw_fenfee_log',array('uniacid'=>$_SESSION['uniacid'],'infeeid'=>$infee_log['id']));
		if($isadd['id'] && $infee_log['pay_type']==1){
			return false;
		}
		if($isadd['id'] && $infee_log['pay_type']!=1){
			pdo_update('jy_yxw_fenfee_log',array('fee'=>$infee_log['daifee'],'time'=>$infee_log['daiday'],'timenum'=>strtotime($infee_log['daiday'])),array('isdai'=>1,'uniacid'=>$_SESSION['uniacid'],'infeeid'=>$infee_log['id']));
			return false;
		}
		if($infee_log['pay_type']==3){
			if($infee_log['daifee']<=0){
				return false;
			}
			$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'memberid'=>$infee_log['memberid'],
					'storeid'=>$infee_log['storeid'],
					'infeeid'=>$infee_log['id'],
					'sn'=>'贷款',
					'fee'=>$infee_log['daifee'],
					'time'=>$infee_log['daiday'],
					'timenum'=>strtotime($infee_log['daiday']),
					'isdai'=>1,
					'createtime'=>time()
			);
			pdo_insert('jy_yxw_fenfee_log',$data);
		}else{
			if(!$infee_log['fen_log']){
				return false;
			}
			$fen_log=$infee_log['fen_log'];
			foreach($fen_log as $fv){
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'memberid'=>$infee_log['memberid'],
					'storeid'=>$infee_log['storeid'],
					'infeeid'=>$infee_log['id'],
					'sn'=>$fv['sn'],
					'fee'=>$fv['fee'],
					'time'=>$fv['time'],
					'timenum'=>$fv['timenum'],
					'createtime'=>time()
				);
				pdo_insert('jy_yxw_fenfee_log',$data);
			}
		}
		return true;
	}
	public function delfenfee_log($infee_log=array()){
		pdo_delete('jy_yxw_fenfee_log',array('uniacid'=>$_SESSION['uniacid'],'infeeid'=>$infee_log['id']));
		return true;
	}
	public function upfankui_log($post){
		$data=array(
			'remark'=>$post['remark'],
			'miannum'=>$post['miannum'],
			'offernum'=>$post['offernum'],
			'fankuigs'=>$post['fankuigs'],
			'fankuigw'=>$post['fankuigw'],
			'fankuitime'=>$post['fankuitime'],
			'fankuifee1'=>$post['fankuifee1'],
			'fankuifee2'=>$post['fankuifee2']
		);
		if($post['status']==14){
			$data['fankuitype']=1;
		}else{
			$data['fankuitype']=0;
		}
		if($post['id']){
			$data['up_time']=time();
			pdo_update('jy_yxw_fankui_log',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['workerid']=$_SESSION['workerid'];
			$data['yonghuid']=$post['yonghuid'];
			$data['storeid']=$_SESSION['storeid'];
			$data['classid']=$this->session('classid');
			$data['status']=$post['status'];
			$data['createtime']=time();
			pdo_insert('jy_yxw_fankui_log',$data);
		}
		return true;
	}
	public function updangan($post,$memberid=0){
		$data=array(
			'pay_type'=>intval($post['pay_type']),
			'fen_file'=>trim($post['fen_file']),
			'danganimage1'=>trim($post['danganimage1']),
			'danganimage2'=>trim($post['danganimage2']),
			'px_file'=>trim($post['px_file']),
			'rx_file'=>trim($post['rx_file']),
			'jj_file'=>trim($post['jj_file']),
		);
		$dangan=pdo_get('jy_yxw_dangan',array('memberid'=>$memberid,'uniacid'=>$_SESSION['uniacid']));
		if($dangan['id']){
			pdo_update('jy_yxw_dangan',$data,array('memberid'=>$memberid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['memberid']=$memberid;
			$data['createtime']=time();
			pdo_insert('jy_yxw_dangan',$data);
		}
		return true;
	}
	public function upyonghulog($post){
		$data=array(
			'type'=>trim($post['type']),
			'workerid'=>$_SESSION['workerid'],
			'storeid'=>$post['storeid'],
			'classid'=>trim($post['classid']),
			'tostoreid'=>trim($post['tostoreid']),
			'toclassid'=>trim($post['toclassid']),
			'remark'=>trim($post['remark']),
			'images'=>ijson_encode($post['images']),
			'keshi'=>trim($post['keshi']),
			'lastkeshi'=>trim($post['lastkeshi']),
			'iskeshi'=>trim($post['iskeshi']),
			'lastfee'=>trim($post['lastfee']),
			'onefee'=>trim($post['onefee']),
			'bufee'=>trim($post['bufee']),
			'danfee'=>trim($post['danfee']),
			'buygoodsday'=>trim($post['buygoodsday']),
			'tuifee'=>trim($post['tuifee']),
			'fukeshi'=>trim($post['fukeshi']),
			'fukeshi1'=>trim($post['fukeshi1']),
			'fukeshi2'=>trim($post['fukeshi2']),
			'fudaytime'=>trim($post['fudaytime']),
			'today'=>trim($post['today']),
			'todaytime'=>strtotime($post['today']),
			'setday'=>trim($post['setday']),
			'setdaytime'=>strtotime($post['setday']),
			'setstatus'=>trim($post['setstatus']),
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['yonghuid']=$post['id'];
		$data['createtime']=time();
		pdo_insert('jy_yxw_yonghu_log',$data);
		return true;
	}
	public function upworkerlog($member,$type=0,$ischeck=false,$status=0,$nomore=false){
		if(!$member){
			return false;
		}
		$data=array(
			'status'=>$status,
		);
		$params=array('type'=>$type,'memberid'=>$member['id'],'uniacid'=>$_SESSION['uniacid']);
		if(!$type){
			$params['worker3id']=$member['worker3id'];
			$params['worker4id']=$member['worker4id'];
		}elseif($type==1){
			$params['worker5id']=$member['worker5id'];
		}elseif($type==4){
			$params['worker5id']=$member['worker5id'];
		}elseif($type==32){
			$params['worker6id']=$member['worker6id'];
		}elseif($type==40 || $type==42){
			$params['worker7id']=$member['worker7id'];
		}elseif($type==41){
			$params['worker8id']=$member['worker8id'];
		}
		$workerlog=pdo_get('jy_yxw_workerlog',$params);
		if($workerlog['id']){
			if(!$ischeck){
				if($status==-1){
					$data['failtime']=time();
				}
				pdo_update('jy_yxw_workerlog',$data,array('id'=>$workerlog['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}else{
			$data['createtime']=time();
			$member=pdo_get('jy_yxw_member',array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
			if($type==0){
				$data['createtime']=$member['createtime']?:time();
			}elseif($type==1){
				$data['createtime']=$member['createtime']?:time();
			}elseif($type==4){
				$data['createtime']=$member['zhun_time']?:time();
			}elseif($type==32){
				$data['createtime']=$member['zhun_time']?:time();
				$workerlog=pdo_get('jy_yxw_workerlog',array('type'=>$type,'memberid'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
				$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
				if($workerlog['createtime']>$daytime || $workerlog['createtime']==$data['createtime']){
					pdo_update('jy_yxw_workerlog',array('worker6id'=>$member['worker6id']),array('type'=>$type,'memberid'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
					return false;
				}
			}elseif($type==40){
				$data['createtime']=$member['ti5_time']?:time();
			}elseif($type==41){
				$data['createtime']=$member['ti7_time']?:time();
			}elseif($type==42){
				$data['createtime']=$member['ti6_time']?:time();
			}
			$data['uniacid']=$_SESSION['uniacid'];
			$data['worker3id']=$member['worker3id'];
			$data['worker4id']=$member['worker4id'];
			$data['worker5id']=$member['worker5id'];
			$data['worker6id']=$member['worker6id'];
			$data['worker7id']=$member['worker7id'];
			$data['worker8id']=$member['worker8id'];
			$data['memberid']=$member['id'];
			$data['type']=$type;
			$data['from_type']=$member['from_type'];
			$data['storeid']=$member['storeid'];
			$data['uptime']=time();
			$data['fromurl']=$_AR['access_json'];
			pdo_insert('jy_yxw_workerlog',$data);
		}
		return true;
	}
	public function upworkerlog2($op='',$memberid=0,$genjin_type=0,$status=0){
		if(!$memberid){
			return false;
		}
		$post['status']=$status;
		if($op=='set9'){	
		}elseif($op=='set5'){
		}elseif($op=='set7'){
			if($post['status']==9){
			}elseif($post['status']==5){
				$logstatus=-1;
				$logtype=40;
			}elseif($post['status']==7){
				$logstatus=-1;
				$logtype=40;
			}elseif($post['status']==6){
				$logstatus=-1;
				$logtype=40;
			}elseif($post['status']==10){
				$logstatus=-1;
				$logtype=42;
			}elseif($post['status']==8){
				$logtype=40;
			}elseif($post['status']==11){
				$logtype=42;
			}elseif($post['status']==12){
				
			}
		}elseif($op=='set8'){
			if($post['status']==13 || $post['status']==16){
				$logstatus=-1;
				$logtype=41;
			}elseif($post['status']==18){
				$logstatus=-1;
				$logtype=41;
			}elseif($post['status']==17){
				$logstatus=-1;
				$logtype=41;
			}elseif($post['status']==14){
				$logtype=41;
			}elseif($post['status']==15 || $genjin_type==11){
			}
		}elseif($op=='set9'){
		}elseif($op=='set6' || $op=='set10'){
		}
		if($post['status']==1 && $this->genjin_totype[$genjin_type]){
			$status=$this->genjin_totype[$genjin_type];
			if($status==4 || $status==40 || $status==41 || $status==42){
				$logtype=$status;
			}
		}
		if($logtype){
			$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$memberid));
			$this->upworkerlog($item,$logtype,true);
			if($logstatus==-1){
				$this->upworkerlogstatus($memberid,$logstatus,$logtype);
			}
		}
		return true;
	}
	public function upworkerlogstatus($memberid=0,$status=0,$logtype=0){
		if(!$memberid){
			return false;
		}
		$update=array('status'=>$status);
		if($status==-1){
			$update['failtime']=time();
		}
		$params=array('memberid'=>$memberid,'uniacid'=>$_SESSION['uniacid']);
		if($logtype){
			$params['type']=$logtype;
		}
		pdo_update('jy_yxw_workerlog',$update,$params);
		return true;
	}
}
?>